#include "game.h"
void init_board (char board[ROWS][COLS],int rows,int cols,char set)
{
    int i=0;
    for (i=0;i<rows;i++)
    {
        int j=0;
        for (j=0;j<cols;j++)
        {
            board[i][j]=set;
        }
    }
}

void display_board (char board[ROWS][COLS],int row,int col)
{
    printf ("\n------扫雷游戏------\n");
    int i=0;
    for (i=0;i<=row;i++)
        printf ("%d ",i);
    printf ("\n");
    
    for (i=1;i<=row;i++)
    {
        printf ("%d ",i);
        int j=0;
        for (j=1;j<=col;j++)
            printf ("%c ",board[i][j]);
        printf ("\n");
    }
    printf ("--------------------\n\n");

}

void set_mine (char mine[ROWS][COLS],int row,int col)
{
    int count =EASY_COUNT;
    while (count)
    {
        int x=rand()%row+1;
        int y=rand()%col+1;
        if (mine[x][y]=='0')
        {
           mine[x][y] ='1';
           count--;
        }
    }
}

int surrounding (char mine[ROWS][COLS],int x,int y)
{
    return (mine[x-1][y-1]+
            mine[x-1][y]+
            mine[x-1][y+1]+
            mine[x][y-1]+
            mine[x][y+1]+
            mine[x+1][y-1]+
            mine[x+1][y]+
            mine[x+1][y+1] -8*'0');
}

void find_mine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
    int x=0;
    int y=0;
    int win=0;
    while (win<row*col-EASY_COUNT)
    {
        printf ("请输入要排查的坐标-->\n");
        scanf ("%d %d",&y,&x);
        //这里我习惯先输入横坐标，也就是列，所以位置换了一下
        if(x>0&&x<=row&&y>0&&y<=col)
        {
            if(mine[x][y]=='1')
            {
                printf ("很遗憾，你被炸死了\n");
                display_board (mine,ROW,COL);
                break;
            }
            else
            {
                int count = surrounding(mine,x,y);
                show [x][y]=count+'0';
                 display_board (show,ROW,COL);
                win++;
            }
        }
        else
            printf ("坐标不存在,请重新输入-->\n");
    }
    if(win == row*col-EASY_COUNT)
    {
        printf ("恭喜你，排雷成功！\n");
        display_board (mine,ROW,COL);
    }
}
